package com.djmw.mouse.entity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.validator.constraints.Range;

import com.djmw.mouse.service.constants.Constants;

@Entity
@Table(name = "t_user_member")
@Inheritance(strategy = InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name = "c_id", referencedColumnName = "c_id")
public class MemberUser extends User {

    public static final char GENDER_MALE = 'm';
    public static final char GENDER_FEMALE = 'f';

    private String realName;
    private Character gender;
    private String birthday;
    private Date birthdayDate;
    private Integer workingYears;
    private Integer certType;
    private String certNum;
    private Integer residence;
    private String email;
    private Integer incomingRange;
    private Integer incomingCurrency;
    private Integer situation; // 求职状态
    private String cellNum;
    private Boolean cellVerified;
    private String companyPhoneNum;
    private String homePhoneNum;
    private Integer hukou;
    private String keyword;
    private Integer country;
    private Integer marriageStatus;
    private Integer height;
    private Integer politicalStatus; // 政治面貌
    private String zipcode;
    private String address;
    private String homepage;
    private String qq;
    private String wangwang;
    private String sinaWeibo;
    private String weChat;

    @Column(name = "c_realname")
    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    @Column(name = "c_gender")
    public Character getGender() {
        return gender;
    }
    public void setGender(Character gender) {
        this.gender = gender;
    }
    @Transient
    public String getGenderName() {
        return gender == null || gender == 'm' ? "男" : "女";
    }

    @Column(name = "c_birthday")
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        if (birthday == null) {
            this.birthday = birthday;
            this.birthdayDate = null;
        }
        this.birthday = birthday.trim();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            birthdayDate = sdf.parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
            birthdayDate = null;
        }
    }
    @Transient
    public int getAge() {
        if (birthdayDate == null) {
            return 0;
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            int today = Integer.parseInt(sdf.format(new Date()));
            int birthdayDay = Integer.parseInt(sdf.format(birthdayDate));
            int result = today / 10000 - birthdayDay / 10000;
            if (today % 10000 > birthdayDay % 10000) {
                result++;
            }
            return result;
        } catch (Exception err) {
            err.printStackTrace();
            return 0;
        }
    }

    @Range(min = 0, max = 8)
    @Column(name = "c_working_years")
    public Integer getWorkingYears() {
        return workingYears;
    }
    public void setWorkingYears(Integer workingYears) {
        this.workingYears = workingYears == null ? 0 : workingYears.intValue();
    }
    @Transient
    public String getWorkingYearsName() {
        return workingYears == 0 ? "" : Constants.WORKING_YEARS_MEMBER.get(workingYears).getName();
    }

    @Column(name = "c_cert_type")
    public Integer getCertType() {
        return certType;
    }

    public void setCertType(Integer certType) {
        this.certType = certType == null ? 0 : certType.intValue();
    }

    @Column(name = "c_cert_num")
    public String getCertNum() {
        return certNum;
    }

    public void setCertNum(String certNum) {
        this.certNum = certNum;
    }

    @Column(name = "c_residence")
    public Integer getResidence() {
        return residence;
    }

    public void setResidence(Integer residence) {
        this.residence = residence == null ? 0 : residence.intValue();
    }

    @Column(name = "c_email")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name = "c_income")
    public Integer getIncomingRange() {
        return incomingRange;
    }

    public void setIncomingRange(Integer incomingRange) {
        this.incomingRange = incomingRange == null ? 0 : incomingRange.intValue();
    }

    @Column(name = "c_income_cur")
    public Integer getIncomingCurrency() {
        return incomingCurrency;
    }

    public void setIncomingCurrency(Integer incomingCurrency) {
        this.incomingCurrency = incomingCurrency == null ? 0 : incomingCurrency.intValue();
    }

    @Column(name = "c_situation")
    public Integer getSituation() {
        return situation;
    }

    public void setSituation(Integer situation) {
        this.situation = situation == null ? 0 : situation.intValue();
    }

    @Column(name = "c_cell")
    public String getCellNum() {
        return cellNum;
    }

    public void setCellNum(String cellNum) {
        this.cellNum = cellNum;
    }

    @Column(name = "c_cell_verified")
    public Boolean getCellVerified() {
        return cellVerified;
    }

    public void setCellVerified(Boolean cellVerified) {
        this.cellVerified = cellVerified == null ? false : cellVerified.booleanValue();
    }

    @Column(name = "c_company_phone")
    public String getCompanyPhoneNum() {
        return companyPhoneNum;
    }

    public void setCompanyPhoneNum(String companyPhoneNum) {
        this.companyPhoneNum = companyPhoneNum;
    }

    @Column(name = "c_home_phone")
    public String getHomePhoneNum() {
        return homePhoneNum;
    }

    public void setHomePhoneNum(String homePhoneNum) {
        this.homePhoneNum = homePhoneNum;
    }

    @Column(name = "c_hukou")
    public Integer getHukou() {
        return hukou;
    }

    public void setHukou(Integer hukou) {
        this.hukou = hukou == null ? 0 : hukou.intValue();
    }

    @Column(name = "c_keyword")
    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    @Column(name = "c_country")
    public Integer getCountry() {
        return country;
    }

    public void setCountry(Integer country) {
        this.country = country == null ? 0 : country.intValue();
    }

    @Column(name = "c_marriage")
    public Integer getMarriageStatus() {
        return marriageStatus;
    }

    public void setMarriageStatus(Integer marriageStatus) {
        this.marriageStatus = marriageStatus == null ? 0 : marriageStatus.intValue();
    }

    @Column(name = "c_height")
    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height == null ? 0 : height.intValue();
    }

    @Column(name = "c_political_status")
    public Integer getPoliticalStatus() {
        return politicalStatus;
    }

    public void setPoliticalStatus(Integer politicalStatus) {
        this.politicalStatus = politicalStatus == null ? 0 : politicalStatus.intValue();
    }

    @Column(name = "c_zipcode")
    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }

    @Column(name = "c_address")
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Column(name = "c_homepage")
    public String getHomepage() {
        return homepage;
    }

    public void setHomepage(String homepage) {
        this.homepage = homepage;
    }

    @Column(name = "c_qq")
    public String getQq() {
        return qq;
    }

    public void setQq(String qq) {
        this.qq = qq;
    }

    @Column(name = "c_wangwang")
    public String getWangwang() {
        return wangwang;
    }

    public void setWangwang(String wangwang) {
        this.wangwang = wangwang;
    }

    @Column(name = "c_sina_weibo")
    public String getSinaWeibo() {
        return sinaWeibo;
    }

    public void setSinaWeibo(String sinaWeibo) {
        this.sinaWeibo = sinaWeibo;
    }

    @Column(name = "c_wechat")
    public String getWeChat() {
        return weChat;
    }

    public void setWeChat(String weChat) {
        this.weChat = weChat;
    }

    public void updateBaseInfo(MemberUser memberUser) {
        realName = memberUser.getRealName();
        gender = memberUser.getGender();
        birthday = memberUser.getBirthday();
        workingYears = memberUser.getWorkingYears();
        certType = memberUser.getCertType();
        certNum = memberUser.getCertNum();
        residence = memberUser.getResidence();
        email = memberUser.getEmail();
        incomingRange = memberUser.getIncomingRange();
        incomingCurrency = memberUser.getIncomingCurrency();
        situation = memberUser.getSituation();
        cellNum = memberUser.getCellNum();
        cellVerified = memberUser.getCellVerified();
        companyPhoneNum = memberUser.getCompanyPhoneNum();
        homePhoneNum = memberUser.getHomePhoneNum();
        hukou = memberUser.getHukou();
        keyword = memberUser.getKeyword();
        country = memberUser.getCountry();
        marriageStatus = memberUser.getMarriageStatus();
        height = memberUser.getHeight();
        politicalStatus = memberUser.getPoliticalStatus();
        zipcode = memberUser.getZipcode();
        address = memberUser.getAddress();
        homepage = memberUser.getHomepage();
        qq = memberUser.getQq();
        wangwang = memberUser.getWangwang();
        sinaWeibo = memberUser.getSinaWeibo();
    }

}
